package com.gopay.cron.event.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.gopay.common.domain.report.Event;
import com.gopay.common.event.handler.EventHandler;
import com.gopay.common.exception.GopayException;
import com.gopay.common.report.dao.MqEventInfoDAO;
import com.gopay.cron.event.manager.EventHandlerManager;
import com.gopay.cron.service.trans.ReportConsoleTransQueryService;
import com.gopay.cron.web.vo.TransQueryResultVO;

/**
 * 
 * @ApprovalDecisionEventDefaultHandler.java
 * @author zhaoxubing
 * @2015年6月5日 下午2:59:16  www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "approvalDecisionEventDefaultHandler")
public class ApprovalDecisionEventDefaultHandler implements EventHandler {
    @Resource 
    private ReportConsoleTransQueryService reportConsoleTransQueryService;
    @Resource(name="mqEventInfoDAO")
    private MqEventInfoDAO mqEventInfoDAO;
    
    @Resource(name = "createtEventHandlerManager")
    private EventHandlerManager createtEventHandlerManager;
    private Logger log = LoggerFactory.getLogger(ApprovalDecisionEventDefaultHandler.class);
    
    @Override
    public void handler(Event e) throws GopayException {
        Map<String, Object> map =StringUtils.isNotBlank(e.getSnapshot())? (Map<String, Object>) JSONObject.parse(e.getSnapshot()):new HashMap<String, Object>();
        if(StringUtils.isNotBlank(e.getGopayOrderId())){
            map.put("gopayOrderId", e.getGopayOrderId());
        }
        List<TransQueryResultVO> transQueryResultList = null;
        try {
            transQueryResultList = reportConsoleTransQueryService.getTransQueryResultList(map);
            createtEventHandlerManager.saveOrUpdateOrderAndEvent(transQueryResultList, e);
        } catch (GopayException e1) {
            log.error("ApprovalDecisionEventDefaultHandler 执行出错 e.gopayOrderId=" + e.getGopayOrderId(), e1);
            throw e1;
        }
    }

}
